package cn.singleton.csl;

/**
 * Created by siyuzhiyue on 2017/6/9.
 */
public class Singleton {
    private static Singleton singleton;
    private  int i = 0;
    private static int count = 0;
    private Singleton(){
        System.out.println("此类被实例化第"+ ++count + "次！");
    }
    public static Singleton getSingleton(){
        if(singleton == null){
            synchronized (Singleton.class){
                if(singleton == null){
                    singleton =  new Singleton();
                }
            }
        }
        return singleton;
    }

    public void printMessage(){
        synchronized (Singleton.class){
            System.out.println(i++);
        }
    }

    public static void main(String[] args){
        getSingleton().printMessage();
        getSingleton().printMessage();
    }
}
